package cn.wizzer.app.web.modules.controllers.user;

import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.UserPurchase;
import cn.wizzer.app.user.modules.models.enums.PurchaseEnum;
import cn.wizzer.app.user.modules.services.UserPurchaseService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.framework.base.exceptions.BizException;
import cn.wizzer.framework.base.model.enums.BizExceptionEnum;
import cn.wizzer.framework.filter.ParamFilter;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.*;

@IocBean
@At("/api/user/purchase")
public class ApiUserPurchaseController {
    @Inject
    @Reference(retries = 0)
    private UserPurchaseService purchaseService;
    @Inject
    @Reference
    private UserService userService;

    /**
     * 购买商品
     *
     * @param userPurchase UserExchange
     * @return Object
     */
    @At("/doPurchase")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object doPurchase(@Param("..") UserPurchase userPurchase) {
        String appid = Mvcs.getReq().getHeader("appid");
        userPurchase.setUserId(appid);
        // 如果用户是游客账号禁止购买
        User user = userService.fetch(appid);
        if (user != null && user.isVisitor()) {
            return Result.error(BizExceptionEnum.ILLEGAL_OPERATION);
        }
        purchaseService.add(userPurchase);
        return Result.success();
    }

    /**
     * 购买记录
     *
     * @param pageNumber int
     * @param pageSize   int
     * @param status     String 订单状态
     * @return Object
     */
    @At("/list")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object list(@ParamVerify(required = true, name = "pageNumber") int pageNumber,
                       @ParamVerify(required = true, name = "pageSize") int pageSize,
                       @ParamVerify(name = "status") PurchaseEnum status) {
        return Result.success().addData(purchaseService.listPage(pageNumber, pageSize, status, Mvcs.getReq().getHeader("appid")));
    }


    /**
     * 订单状态
     *
     * @return Object
     */
    @At("/statusList")
    @Ok("json")
    public Object statusList() {
        return Result.success().addData(PurchaseEnum.toList());
    }

    /**
     * 确认收货
     *
     * @return Object
     */
    @At("/confirm")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object confirm(@ParamVerify(required = true, name = "id") String id) {
        UserPurchase userPurchase = purchaseService.fetch(id);
        if (!userPurchase.getUserId().equals(Mvcs.getReq().getHeader("appid"))) {
            return Result.error(BizExceptionEnum.FAILED_TO_CONFIRM_RECEIPT);
        }
        if (!userPurchase.getStatus().equals(PurchaseEnum.DELIVERY)) {
            return Result.error(BizExceptionEnum.THE_ORDER_IS_NOT_IN_SHIPPED_STATUS);
        }
        purchaseService.update(Chain.make("status", PurchaseEnum.FINISHED), Cnd.where("id", "=", id));
        return Result.success();
    }


}

